<template>
	<view class="cantainer">
		<fui-card >
			<view class="split">
				<view class="color"></view>个人头像
			</view>
			<u-avatar :src="user.userPhotoUrl" size="150" @click="upImage"></u-avatar>
			<view class="split">
				<view class="color"></view>基本信息
			</view>
			<view class="fui-card__content">
				<view class="text-content">
					<view class="text-title">
						<fui-input :bottomLeft="0" placeholder="" maxlength="11" v-model="user.userName" label="用户名称:" borderBottom trim :adjustPosition="false" clearable></fui-input>
					</view>
					<view class="text-title">
						<fui-input :bottomLeft="0" placeholder="" maxlength="11" v-model="user.realName" label="真实姓名:" borderBottom trim :adjustPosition="false" clearable></fui-input>
					</view>
					<view class="text-title">
					<fui-input :bottomLeft="0" placeholder="请输入手机号码" maxlength="11" v-model="user.phone" label="手机号码:" borderBottom trim :adjustPosition="false" clearable></fui-input>
					</view>
					<view class="text-title">
					<fui-input :bottomLeft="0" placeholder="" maxlength="21" v-model="user.email" label="邮箱:" borderBottom trim :adjustPosition="false" clearable></fui-input>
					</view>
					<view class="text-title">
						<fui-input :bottomLeft="0" placeholder="" maxlength="11" v-model="user.createTime" label="创建时间:" borderBottom trim :adjustPosition="false" :disabled="true" backgroundColor="#f5f5f5" ></fui-input>
					</view>
					<view class="text-title">
						<fui-input :bottomLeft="0" placeholder="" maxlength="11" v-model="user.password" label="用户密码:" borderBottom trim :adjustPosition="false" :disabled="true" password backgroundColor="#f5f5f5" @click="changePassword"></fui-input>
					</view>
					<view class="text-title">
						<fui-input :bottomLeft="0" placeholder="" maxlength="11" v-model="user.photoDistinguishNum" label="图片识别剩余次数:" borderBottom trim :adjustPosition="false" :disabled="true" backgroundColor="#f5f5f5"  @click="noEdit"></fui-input>
					</view>
					<view class="text-title">
						<fui-button type="success" @click="saveUser">保存信息</fui-button>
					</view>
				</view>
			</view>
		</fui-card>
		<u-modal :show="show" :closeOnClickOverlay="true" :showCancelButton="true" confirmColor="green" @confirm="confirm" @cancel="show=false" @close="show=false">
			<view class="slot-content">
				<fui-input :bottomLeft="0" placeholder="" maxlength="11" v-model="changePwd.old" label="原密码:" borderBottom trim :adjustPosition="false" clearable ></fui-input>
				<fui-input :bottomLeft="0" placeholder="" maxlength="11"  v-model="changePwd.new" label="新密码:" borderBottom trim :adjustPosition="false" clearable placeholder="数字+大小写字母"></fui-input>
				<fui-input :bottomLeft="0" placeholder="" maxlength="11" v-model="changePwd.reNew" label="确认新密码:" borderBottom trim :adjustPosition="false" clearable placeholder="数字+大小写字母"></fui-input>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		chooseImage,
		previewImage
	} from "@/common/utils/chooseImage.js" //上传图片以及后续读取封装
	import { pathToBase64, base64ToPath } from '@/node_modules/image-tools/index.js'
	export default {
		data() {
			return {
				user:[],
				show:false,
				changePwd:{
					old:'',
					new:'',
					reNew:''
				}
			}
		},
		onLoad() {
			this.initData()
			console.log(this.user)
		},
		methods: {
			initData(){
				this.user = JSON.parse(this.$storage.get("user"))
			},
			confirm(data){
				if(this.changePwd.new != this.changePwd.reNew){
					this.$utils.errorMsg("两次密码不一致")
					return
				}
				let json ={
					oldPwd:this.changePwd.old,
					newPwd:this.changePwd.new,
					userId:this.user.id
				}
				console.log(JSON.stringify(json))
				this.$http.httpPost('user/changePwd',JSON.stringify(json)).then(sucess=>{
					if(sucess.data.status == 200){
						this.$utils.sucessMsg("修改成功")
						this.user.password = sucess.data.data
						this.show=false
					}
				}).catch(error=>{
					this.$utils.errorMsg(error.data.msg)
				})
			},
			changePassword(data){
				this.show=true
			},
			noEdit(data){
				this.$utils.errorMsg("不可编辑")
			},
			upImage() {
				// 从本地获取图片
				chooseImage().then(success=>{
					// 将图片转为base64格式,准备传到后端
					pathToBase64(success.tempFilePaths[0]).then(base64Success=>{
						this.$storage.set("userImageBase64",base64Success)
						let userImage = this.user.phone + '-title.jpg'
						let userAndImage = {
							"base64":base64Success.slice(23,-1),
							"user":this.user
						}
						//到后台用户图片保存API接口
						this.$http.httpPost('photo/upUserImage/'+userImage,userAndImage).then(upSuccess=>{
							// 分割传回的字符串,获取新的用户头像URL
							let url = upSuccess.data.data.slice(7)
							userAndImage.user.userPhotoUrl = url
							this.user.userPhotoUrl = base64Success
							// 写入到Storage中
							this.$storage.setJson("user",userAndImage.user)
							this.$utils.msg(upSuccess.data.data.slice(0,6))
							// 刷新
							// this.$utils.reLaunch("../reLaunch/reLaunch?id=person")
						})
					})
				})
			},
			saveUser(){
				this.$http.httpPost('user/updateUser',this.user).then(sucess=>{
						console.log(sucess)
						if(sucess.data.status==200){
							this.$utils.sucessMsg(sucess.data.data)
							this.$storage.setJson("user",this.user)
						}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.cantainer{
	margin: 20rpx;
}
.split{
	margin: 50rpx 20rpx;
	color:#3b4244 ;
	font-weight: bold;
	.color{
		display: inline-block;
		width: 8rpx;
		height: 25rpx;
		background-color: #348669;
		margin-right: 5rpx;
	}
}
.text-content{
	margin-top: 20rpx;
	box-shadow:5 5 5 #000000;
	.text-title{
		margin: 30rpx 10rpx;
		color: #8b8b8b;
	}
}
</style>
